package com.ifly.voice.activity;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.speech.RecognitionService;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.util.LogUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;
import com.ifly.voice.R;
import com.ifly.voice.app.App;
import com.ifly.voice.app.Logs;
import com.ifly.voice.recognizer.RecognitionListener;
import com.ifly.voice.recognizer.RecognizerTaskHelper;
import com.ifly.voice.recognizer.VoiceAdapter;
import com.ifly.voice.service.IVoiceListener;
import com.ifly.voice.service.VoiceService;

public class DetailActivity extends Activity
{

	@ViewInject(R.id.title)
	TextView title;
	@ViewInject(R.id.soundInfo)
	TextView soundInfo;
	@ViewInject(R.id.backPanel)
	LinearLayout backPanel;
	
	@OnClick(R.id.backPanel)
	public void backPanelClick(View view){
		finish();
	}
	
	IVoiceListener iVoiceListener;
	private DetailActivity context;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_detail);
		ViewUtils.inject(this);
		Intent intent = getIntent();
		String stringExtra = intent.getStringExtra("aa");
		title.setText(stringExtra);
		context = this;
		App app = (App) getApplication();
		iVoiceListener = app.getiVoiceListener();
		
	}

	@Override
	protected void onStart()
	{

		super.onStart();

	}

	@Override
	protected void onResume()
	{
		Logs.d("de  iVoiceListener = " + iVoiceListener);
		if (iVoiceListener != null)
		{
			iVoiceListener.setRecognitionListener(recognitionListener);
			iVoiceListener.startListen();
		}
		super.onResume();

	}

	@Override
	protected void onPause()
	{
		if (iVoiceListener != null)
		{
			iVoiceListener.stopListen();
		}
		super.onPause();
	}

	@Override
	protected void onStop()
	{
		super.onStop();

	}

	protected void onDestroy()
	{
		super.onDestroy();
	};

	RecognitionListener recognitionListener = new RecognitionListener()
	{

		public String toString()
		{
			return "detail recognitionListener" + recognitionListener.hashCode();
		};

		@Override
		public void onResults(Bundle b)
		{

		}

		@Override
		public void onPartialResults(Bundle b)
		{
			final String hyp = b.getString("hyp");
			Logs.d("onPartialResults hyp=" + hyp);
			if (hyp == null)
			{
				return;
			}
			soundInfo.post(new Runnable()
			{

				@Override
				public void run()
				{
					soundInfo.setText(hyp);

					int id = VoiceAdapter.getIdentify(hyp);
					Logs.d("id=" + id);
					if (id == -1)
					{
						return;
					}
					if (id == VoiceAdapter.EXIT)
					{
						VoiceAdapter.doEXIT(context);
					}
					else if (id == VoiceAdapter.BACK)
					{
						backPanel.setPressed(true);
						finish();
					}
				}
			});

		}

		@Override
		public void onError(int err)
		{
		}
	};

}
